package com.andy.web.user.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.andy.domain.UserStatus;
import com.andy.domain.model.User;
import com.andy.domain.result.BaseResult;
import com.andy.util.CharactorUtil;
import com.andy.util.ModelUtil;
import com.andy.web.user.dao.UserMapper;
import com.andy.web.util.MailUtil;
import com.andy.web.util.SessionUtil;

@Service
public class UserService {
	
	@Resource
	private UserMapper userMapper;
	
	public BaseResult reg(String username, String password, String email, String province, String city) {
		BaseResult res = new BaseResult();
		User user = userMapper.fetchUserByUsername(username);
		if(user!=null) {
			res.setCode(5001);
			res.setMessage("账号已经存在");
		}
		user = userMapper.fetchUserByEmail(email);
		if(user!=null) {
			res.setCode(5001);
			res.setMessage("邮箱已经存在");
		}
		
		List<User> list = new ArrayList<User>();
		user = new User();
		user.setUsername(username);
		user.setPassword(password);
		user.setEmail(email);
		user.setProvince(province);
		user.setCity(city);
		String activationCode = CharactorUtil.generateNBitsNumbersOrCharacters(255);
		user.setActivationCode(activationCode);
		user.setRegTime(System.currentTimeMillis()/1000);
		
		ModelUtil.fillNullValueField(user);
		list.add(user);
		
		userMapper.addNewUser(list);
		boolean sendSuccess = MailUtil.sendMailWithText(email, "激活邮件", "请点击以下链接激活:<br/> http://127.0.0.1/user/checkActivationCode/"+username+"/"+activationCode);
		if(sendSuccess == Boolean.FALSE) {
			res.setCode(5000);
			res.setMessage("服务器错误");
			return res;
		}
		
		res.setCode(200);
		res.setMessage("注册成功");
		
		return res;
	}

	public BaseResult login(String username, String password) {
		BaseResult  res = new BaseResult();
		User user = userMapper.fetchUserByUsername(username);
		if(user != null) {
			if(user.getStatus()!= UserStatus.NORMAL.getValue()) {
				res.setCode(5002);
				res.setMessage("账户未激活");
				return res;
			}
			if( StringUtils.isEmpty(password)|| ( !password.equals(user.getPassword()) )) {
				res.setCode(5004);
				res.setMessage("账号或密码错误");
				return res;
			}
			res.setCode(200);
			res.setMessage("登录成功");
			SessionUtil.setCurrentUser(user);
			return res;
		} 
		
		res.setCode(5003);
		res.setMessage("账号不存在");
		return res;
	}

	public boolean checkActivationCode(String username, String actCode) {
		boolean res = false;
		User user = userMapper.fetchUserByUsername(username);
		if(user != null && user.getStatus() == UserStatus.NOT_ACTIVATED.getValue() && !StringUtils.isEmpty(actCode) && actCode.equals(user.getActivationCode()) ) {
			res = true;
			userMapper.updateStatusById(user.getId(), UserStatus.NORMAL.getValue());
		}
		
		return res;
	}
}
